package com.bpf.base.controller.system;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bpf.base.base.vo.TreeNode;
import com.bpf.base.cache.AppService;
import com.bpf.base.common.Constants;
import com.bpf.base.controller.BaseController;
import com.bpf.base.model.system.Dict;
import com.bpf.base.service.system.DictService;
import com.bpf.base.util.AjaxUtils;
import com.bpf.base.util.Collections3;

@Controller
@RequestMapping( "/system/dict" )
public class DictController extends BaseController {

	@SuppressWarnings("unused")
	private final static Log log = LogFactory.getLog(DictController.class);

	@Autowired
	private DictService dictService;

	@Autowired
	private AppService appService;

	@RequestMapping(value = { "/init", "" })
	public String init() {
		return "system/dict";
	}

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request, Dict dictSearch) {
		Integer pageNum = Integer
				.parseInt(null == request.getParameter("page") ? 1 + ""
						: request.getParameter("page").toString());
		Integer pageSize = Integer.parseInt(null == request
				.getParameter("rows") ? 10 + "" : request.getParameter("rows")
				.toString());
		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put(Constants.Page.OFFSET, (pageNum - 1) * pageSize);
		conditions.put(Constants.Page.LIMIT, pageSize);
		conditions.put(Constants.Condition.ORDERBY_CLAUSE, "TYPE DESC");

		List<Dict> list = dictService.selectList(conditions);
		int total = dictService.selectCount(conditions);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("page", pageNum);
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String, Object> save(Dict dict) {
		if(dict.getType() != null && !"".equals(dict.getType())){
			dict.setCode((dictService.selectMaxCode(dict.getType())+1)+"");
			dict.setSequence(dictService.selectMaxCode(dict.getType())+1);
		}
		if (dictService.insertSelective(dict) > 0) {
			appService.asMap().put(dict.getType(), dictService.selectByType(dict.getType()));
			return AjaxUtils.reponseToJson(Constants.Action.SAVE, true);
		}
		return AjaxUtils.reponseToJson(Constants.Action.SAVE, false);
	}

	@ResponseBody
	@RequestMapping(value = "/update")
	public Map<String, Object> update(Dict dict) {
		if (dictService.updateByPrimaryKeySelective(dict) > 0) {
			appService.asMap().put(dict.getType(), dictService.selectByType(dict.getType()));
			return AjaxUtils.reponseToJson(Constants.Action.UPDATE, true);
		}
		return AjaxUtils.reponseToJson(Constants.Action.UPDATE, false);

	}

	@RequestMapping(value = "/deleteAll")
	@ResponseBody
	public Map<String, Object> delete(
			@RequestParam(value = "dictId", required = true) Integer dictId) {
		Dict temp_dict=dictService.selectByPrimaryKey(dictId);
		String type=temp_dict.getType();
		if (dictService.deleteByPrimaryKey(dictId) > 0) {			
			//先移除该key下的缓存
			appService.invalidate(type);
			if(!Collections3.isEmpty(dictService.selectByType(type))){				
				appService.asMap().put(type, dictService.selectByType(type));
			}
			return AjaxUtils.reponseToJson(Constants.Action.DELETE, true);
		}
		return AjaxUtils.reponseToJson(Constants.Action.DELETE, false);

	}
	
	@RequestMapping("/tree")
	@ResponseBody
	public List<TreeNode> tree(String type){
		List<TreeNode> tree=new ArrayList<TreeNode>();
		List<Dict> list = dictService.selectByType(type);
		if(!Collections3.isEmpty(list)){
			for(Dict dict :list){
				TreeNode Tin=new TreeNode();
				Tin.setId(dict.getCode());
				Tin.setText(dict.getValue());
				Tin.setParentId("PLATFORM_TYPE");
				tree.add(Tin);
			}
		}
		return tree;
	}

}
